package kgame5.kgu.move.quadtree {
import kgame5.k3d.core.obj3d.hardvo.HardVO;
import kgame5.kgui.GP;
import kgame5.kutil.T;

import flash.display.Sprite;

/**
 * @author kk
 * 01
 * 23
 */
public class QuadTreeNode{
	
	//父
	public var parentNode:QuadTreeNode=null;
	
	//四个子节点<QuadTreeNode>
	public var aChildNode:Array=new Array(4);
	
	//深度
	public var depth:int=0;
	
	//总格子的大小
	public var rect:XZRectangle=new XZRectangle();
	//四个分区格子的大小
	public var rect0:XZRectangle=new XZRectangle();
	public var rect1:XZRectangle=new XZRectangle();
	public var rect2:XZRectangle=new XZRectangle();
	public var rect3:XZRectangle=new XZRectangle();
	
	//此分区内（不属于子分区的）的物体
	public var listObj:Array=new Array();//包含在此单元格子中的东西，不属于任何一个想想的东西
	
	
	public function QuadTreeNode(x:int,z:int,w:int,h:int){
		rect.x=x;
		rect.z=z;
		rect.w=w;
		rect.h=h;
		
		var w2:int=w>>1;
		var h2:int=h>>1;
		
		rect0.x=x;
		rect0.z=z;
		rect0.w=w2;
		rect0.h=h2;
		
		rect1.x=x+w2;
		rect1.z=z;
		rect1.w=w2;
		rect1.h=h2;
		
		rect2.x=x;
		rect2.z=z-h2;
		rect2.w=w2;
		rect2.h=h2;
		
		rect3.x=x+w2;
		rect3.z=z-h2;
		rect3.w=w2;
		rect3.h=h2;
	}
	
	public function addChildNode(quadIdx:int){
		var node:QuadTreeNode;
		switch(quadIdx){
			case 0:
			node=new QuadTreeNode(rect0.x,rect0.z,rect0.w,rect0.h);
			break;
			case 1:
			node=new QuadTreeNode(rect1.x,rect1.z,rect1.w,rect1.h);
			break;
			case 2:
			node=new QuadTreeNode(rect2.x,rect2.z,rect2.w,rect2.h);
			break;
			case 3:
			node=new QuadTreeNode(rect3.x,rect3.z,rect3.w,rect3.h);
			break;
		}
		
		aChildNode[quadIdx]=node;
		node.parentNode=this;
		node.depth=depth+1;
		
	}
	
	//只跟此node的listObj来碰撞,只需要知道是否有人撞上，不需要精确知道是谁
	public function fastCD(vo:HardVO):Boolean{
		for each(var aimVO:HardVO in listObj){
			if(aimVO.HitTestAABB(vo)){
				//T.p("Node","fastCD",depth,"碰撞上了", vo,aimVO);
				return true;
			}
		}
		return false;
	}
	
	//返回所有与此vo碰撞的东西,结果保存在arr里
	
	public function bestCD(vo:HardVO,arr:Array){
		for each(var aimVO:HardVO in listObj){
			if(aimVO.HitTestAABB(vo)){
				//T.p("Node","bestCD",depth,"碰撞上了", vo,aimVO);
				arr.push(aimVO);
			}
		}
	}
	
	//输出调试信息，是一个mc,测试用的
	public function dump():Sprite{
		T.p(this,depth);
		var mc:Sprite=new Sprite();
		GP.drawRect(mc.graphics, 0xff0000, rect.x, -rect.z, rect.w, rect.h);
		
		GP.drawRect(mc.graphics, 0xff0000, rect0.x, -rect0.z, rect0.w, rect0.h);
		GP.drawRect(mc.graphics, 0xff0000, rect1.x, -rect1.z, rect1.w, rect1.h);
		GP.drawRect(mc.graphics, 0xff0000, rect2.x, -rect2.z, rect2.w, rect2.h);
		GP.drawRect(mc.graphics, 0xff0000, rect3.x, -rect3.z, rect3.w, rect3.h);
		
		for each(var childNode:QuadTreeNode in aChildNode){
			T.p("--");
			if(childNode==null){
				T.p("这个是个空的");
				continue;
			}
			mc.addChild(childNode.dump());
		}
		
//		for(var i:int=0;i<listObj.length;i++){
//			var vo:HardVO=listObj[i] as HardVO;
//			var mc_tank:Sprite=new Sprite();
//			GP.drawRect(mc_tank.graphics, 0xffff00, 0, 0, vo.aabb.A, vo.aabb.B);
//		}
		return mc;
	}
}
}
